package com.hu.green.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hu.green.entity.Comment;
import com.hu.green.entity.Php;
import com.hu.green.entity.User;
import com.hu.green.mapper.PhpMapper;
import com.hu.green.service.CommentService;
import com.hu.green.service.PhpService;
import com.hu.green.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.*;

@Service
public class PhpServiceImpl extends ServiceImpl<PhpMapper, Php> implements PhpService {

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    /**
     * 传入信息
     *
     * @param php
     */

    @Override
    public void getMessage(@RequestBody Php php) {
        // 构建条件构造器
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount, php.getAccount());
        // 获取符合条件的记录
        User user = userService.getOne(queryWrapper);
        // 将用户信息填入
        php.setUserName(user.getName()); // 用户名
        php.setUrl(user.getImg()); // 头像
    }

    /**
     * 对每个帖子的用户信息进行更新
     */
    @Override
    public void upDate() {

        LambdaQueryWrapper<Php> queryWrapper = new LambdaQueryWrapper<>();
        List<Php> phps = this.list(queryWrapper);

        for (Php php : phps) {

            LambdaQueryWrapper<Php> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Php::getId,php.getId());

            Php php1 = this.getOne(queryWrapper1);

            User user = userService.getById(php1.getAccount());

            php1.setUrl(user.getImg());
            php1.setUserName(user.getName());

            this.update(php1,queryWrapper1);
        }

    }

}
